<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是vimscript一些基础知识和操作的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-vimscript-04-变量与表达式.md">
<meta property="og:url" content="https://sumumm.github.io/post/31c064e2.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是vimscript一些基础知识和操作的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-04-%E5%8F%98%E9%87%8F%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F/img/image-20220306100412807.png">
<meta property="article:published_time" content="2023-07-02T07:05:49.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.068Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="VIM">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-04-%E5%8F%98%E9%87%8F%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F/img/image-20220306100412807.png">


<link rel="canonical" href="https://sumumm.github.io/post/31c064e2.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/31c064e2.html","path":"post/31c064e2.html","title":"LV02-vimscript-04-变量与表达式.md"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-vimscript-04-变量与表达式.md | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81vim%E5%8F%98%E9%87%8F"><span class="nav-text">一、vim变量</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B"><span class="nav-text">1. 变量类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-text">2. 变量的定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%88%97%E5%87%BA%E5%8F%98%E9%87%8F"><span class="nav-text">3. 列出变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%88%A0%E9%99%A4%E5%8F%98%E9%87%8F"><span class="nav-text">4. 删除变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%8F%98%E9%87%8F%E8%AF%A6%E8%A7%A3"><span class="nav-text">5. 变量详解</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E6%95%B0%E5%80%BCNumber"><span class="nav-text">5.1 数值Number</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E6%B5%AE%E7%82%B9%E6%95%B0Float"><span class="nav-text">5.2 浮点数Float</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2"><span class="nav-text">5.3 强制转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-%E5%AD%97%E7%AC%A6%E4%B8%B2String"><span class="nav-text">5.4 字符串String</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-1-%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6%E6%89%93%E5%8D%B0"><span class="nav-text">5.4.1 转义字符打印</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-2-%E5%8E%9F%E6%A0%B7%E6%89%93%E5%8D%B0%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">5.4.2 原样打印字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-3-%E8%BF%9E%E6%8E%A5%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">5.4.3 连接字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-4-%E6%88%AA%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">5.4.4 截取字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-5-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%87%BD%E6%95%B0"><span class="nav-text">5.4.5 字符串函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-5-%E5%88%97%E8%A1%A8List"><span class="nav-text">5.5 列表List</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-1-%E5%88%9B%E5%BB%BA%E5%88%97%E8%A1%A8"><span class="nav-text">5.5.1 创建列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-2-%E5%88%97%E8%A1%A8%E7%B4%A2%E5%BC%95"><span class="nav-text">5.5.2 列表索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-3-%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="nav-text">5.5.3 修改元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-4-%E5%88%97%E8%A1%A8%E8%BF%9E%E6%8E%A5"><span class="nav-text">5.5.4 列表连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-5-%E5%AD%90%E5%88%97%E8%A1%A8"><span class="nav-text">5.5.5 子列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-6-%E5%88%97%E8%A1%A8%E5%90%8C%E4%B8%80"><span class="nav-text">5.5.6 列表同一</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-7-%E5%88%97%E8%A1%A8%E8%A7%A3%E5%8C%85"><span class="nav-text">5.5.7 列表解包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-5-8-%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0"><span class="nav-text">5.5.8 列表函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-6-%E5%AD%97%E5%85%B8Dictionary"><span class="nav-text">5.6 字典Dictionary</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-1-%E5%88%9B%E5%BB%BA%E5%AD%97%E5%85%B8"><span class="nav-text">5.6.1 创建字典</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-2-%E5%AD%97%E5%85%B8%E7%B4%A2%E5%BC%95"><span class="nav-text">5.6.2 字典索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-3-%E5%AD%97%E5%85%B8%E4%BF%AE%E6%94%B9"><span class="nav-text">5.6.3 字典修改</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-4-%E5%AD%97%E5%85%B8%E6%B7%BB%E5%8A%A0"><span class="nav-text">5.6.4 字典添加</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-5-%E5%AD%97%E5%85%B8%E7%A7%BB%E9%99%A4"><span class="nav-text">5.6.5 字典移除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-6-%E5%AD%97%E5%85%B8%E5%90%8C%E4%B8%80"><span class="nav-text">5.6.6 字典同一</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-6-7-%E5%AD%97%E5%85%B8%E5%87%BD%E6%95%B0"><span class="nav-text">5.6.7 字典函数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%9F%A5%E7%9C%8B%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B"><span class="nav-text">6. 查看变量类型</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81vim%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">二、vim表达式</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/31c064e2.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-vimscript-04-变量与表达式.md | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-vimscript-04-变量与表达式.md
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 15:05:49" itemprop="dateCreated datePublished" datetime="2023-07-02T15:05:49+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">开发工具</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/" itemprop="url" rel="index"><span itemprop="name">02VIM编辑器</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.5k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>27 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是vimscript一些基础知识和操作的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-04-%E5%8F%98%E9%87%8F%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr>        <td align="left">Learn vimscript the Hard Way</td>        <td align="left"><a href="https://learnvimscriptthehardway.stevelosh.com/" target="_blank">https://learnvimscriptthehardway.stevelosh.com/  <i class="fa fa-external-link-alt"></i> </a></td>      </tr>    <tr>        <td align="left">vim在线文档</td>        <td align="left"><a href="http://vimcdoc.sourceforge.net/" target="_blank">http://vimcdoc.sourceforge.net/  <i class="fa fa-external-link-alt"></i> </a></td>      </tr>    <tr>        <td align="left">VIM 中文帮助</td>        <td align="left"><a href="https://vimcdoc.sourceforge.net/doc/help.html" target="_blank">https://vimcdoc.sourceforge.net/doc/help.html  <i class="fa fa-external-link-alt"></i> </a></td>      </tr>    <tr>        <td align="left">vim教程</td>        <td align="left"><a href="https://www.w3cschool.cn/vim/" target="_blank">https://www.w3cschool.cn/vim/  <i class="fa fa-external-link-alt"></i> </a></td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、vim变量"><a href="#一、vim变量" class="headerlink" title="一、vim变量"></a><font size=3>一、<code>vim</code>变量</font></h1><p>变量一词，对于我来说并不陌生，从<code>C语言</code>到<code>Shell</code>，中间再夹杂一些<code>Html</code>，<code>Javascript</code>还有<code>Python</code>，自己可能只学到了些皮毛，但是各种类型的变量及其声明声明方式还应当是多少有些了解的。这里有一份官方参考文档待查收：<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#eval.txt">VIM: eval  <i class="fa fa-external-link-alt"></i> </a></p>
<h2 id="1-变量类型"><a href="#1-变量类型" class="headerlink" title="1. 变量类型"></a><font size=3>1. 变量类型</font></h2><p>在<code>vim</code>中有多达<code>10</code>种变量，包括了数值变量，浮点数变量，字符串变量，列表变量，字典变量，函数引用变量，特殊变量，作业变量，通道变量和<code>bob</code>变量。</p>
<details class="folding-tag" blue><summary> 点击查看详细变量说明 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>类型</td>        <td align="left">说明</td>    </tr>    <tr>        <td align="center" width=150px>数值<br>(Number)</td>        <td align="left">32 位或 64 位带符号整数。64 位整数只有在编译时带  +num64  特性时才有效。<br>示例:  -123&emsp;&emsp;0x10&emsp;&emsp;0177&emsp;&emsp;0b1011</td>    </tr>    <tr>        <td align="center" width=150px>浮点数<br>(Float)</td>        <td align="left">带小数的数值。{仅当编译时加入  +float  特性才有效}<br>示例: 123.456&emsp;&emsp;1.15e-6&emsp;&emsp;-1.1e3</td>    </tr>    <tr>        <td align="center" width=150px>字符串<br>(String)</td>        <td align="left">NUL 结尾的 8 位无符号字符 (即字节) 的串。<br>示例: "ab\txx\"--"&emsp;&emsp;'x-z' 'a,c'</td>    </tr>    <tr>        <td align="center" width=150px>列表<br>(List)</td>        <td align="left">项目的有序的序列。<br>示例: 示例: [1, 2, ['a', 'b']]</td>    </tr>    <tr>        <td align="center" width=150px>字典<br>(Dictionary)</td>        <td align="left">关联的无序数组: 每个项目包含一个键和一个值。<br>示例: {'blue': "#0000ff", 'red': "#ff0000"}<br>#{blue: "#0000ff", red: "#ff0000"}</td>    </tr>    <tr>        <td align="center" width=150px>函数引用<br>(Funcref)</td>        <td align="left">指向一个函数的引用  Funcref。<br>示例: function("strlen")<br>可以绑定到字典或参数上，这样就类似于一个偏函数。<br>示例: function("Callback", [arg], myDict)</td>    </tr>    <tr>        <td align="center" width=150px>特殊<br>(Special)</td>        <td align="left">v:false, v:true, v:none 和 v:null</td>    </tr>    <tr>        <td align="center" width=150px>作业<br>(Job)</td>        <td align="left">用于作业</td>    </tr>    <tr>        <td align="center" width=150px>通道<br>(Channel)</td>        <td align="left">用于通道</td>    </tr>    <tr>        <td align="center" width=150px>Blob</td>        <td align="left">二进制大对象 (Binary Large Object)，存储任意字符序列。<br>示例: 0zFF00ED015DAF<br>0z 是空 blob。</td>    </tr></table>
              </div>
            </details>

<h2 id="2-变量的定义"><a href="#2-变量的定义" class="headerlink" title="2. 变量的定义"></a><font size=3>2. 变量的定义</font></h2><p>可以通过如下格式定义一个变量，在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:let &#123;variable&#125;=&#123;expression&#125;</span><br></pre></td></tr></table></figure>

<p>这样即可定义一个变量同时进行赋值，变量名可以包含字符、数字和下划线，但必须以字符或是下划线开头。</p>
<p>【说明】在<code>vim</code>官方文档《<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#expression-commands">VIM: eval  - expression-commands  <i class="fa fa-external-link-alt"></i></a>》一节有更为详细的说明。</p>
<details class="folding-tag" blue><summary> 点击查看变量定义前缀含义 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>variable</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" width=150px>$ENVIRONMENT</td>        <td align="left">环境变量<br>示例，:let $MYPATH = "~/.vimrc"</td>    </tr>    <tr>        <td align="center" width=150px>@register</td>        <td align="left">文本寄存器<br>示例，:let @a = "Hello"<br>&emsp;&emsp;&emsp;:echo @a</td>    </tr>    <tr>        <td align="center" width=150px>&option</td>        <td align="left">选项名称，可用于设置布尔选项和键值选项<br>示例，:let &numberwidth = 15<br>&emsp;&emsp;&emsp;:set numberwidth?<br>&emsp;&emsp;&emsp;:echo &numberwidth </td>    </tr>    <tr>        <td align="center" width=150px>b:var_name</td>        <td align="left">局部于当前缓冲区的变量<br>示例，:let b:current_syntax=c</td>    </tr>    <tr>        <td align="center" width=150px>w:var_name</td>        <td align="left">局部于当前窗口的变量</td>    </tr>    <tr>        <td align="center" width=150px>g:var_name</td>        <td align="left">全局变量，常用在函数内部，表明该变量是全局变量</td>    </tr>     <tr>        <td align="center" width=150px>a:var_name</td>        <td align="left">函数参数(只限在函数内部使用)</td>    </tr>    <tr>        <td align="center" width=150px>v:var_name</td>        <td align="left">Vim 内部预定义全局变量</td>    </tr>    <tr>        <td align="center" width=150px>l:var_name</td>        <td align="left">局部于当前函数变量</td>    </tr>    <tr>        <td align="center" width=150px>s:var_name</td>        <td align="left">局部于 s:source 的 Vim 脚本</td>    </tr>    <tr>        <td align="center" width=150px>t:var_name</td>        <td align="left">局部于当前标签页的变量</td>    </tr></table><p>【说明】</p><p>如果 <a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/options.html#'viminfo'">viminfo  <i class="fa fa-external-link-alt"></i></a> 选项包含<code>!</code> 标志位，大写开头且不包含小写字母的全局变量被保存在 <code>viminfo</code> 文件里  <a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/starting.html#viminfo-file">viminfo-file  <i class="fa fa-external-link-alt"></i></a> 。 如果 <a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/options.html#'sessionoptions'">sessionoptions <i class="fa fa-external-link-alt"></i></a>   选项包含 <code>&quot;global&quot;</code>，大写开头且包含至少一个小写字母的全局 变量被保存在会话文件里  <a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/starting.html#session-file">session-file  <i class="fa fa-external-link-alt"></i></a> 。</p><p>关于<code>v:var_name</code>，在官方文档的《<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#vim-variable">VIM: eval - vim-variable  <i class="fa fa-external-link-alt"></i></a>》一节，这里列出几个常用的变量。</p><table>    <tr>        <td align="center" width=100px>变量</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" width=100px>v:count</td>        <td align="left">上一次正常模式命令所指定的数量</td>    </tr>    <tr>        <td align="center" width=100px>v:count1</td>        <td align="left">与v:count类似，所不同的是如果没有指定数量则默认值为1</td>    </tr>    <tr>        <td align="center" width=100px>v:errmsg</td>        <td align="left">上一次的错误信息</td>    </tr>    <tr>        <td align="center" width=100px>v:warningmsg</td>        <td align="left">上一次的警告信息</td>    </tr>    <tr>        <td align="center" width=100px>v:statusmsg</td>        <td align="left">最近给出的状态消息，可以设置该变量</td>    </tr>    <tr>        <td align="center" width=100px>v:version</td>        <td align="left">Vim 的版本号: 主版本号乘以 100 加上副版本号，5.0 版本对应的是500，5.1 版本则是 501，只读。</td>    </tr>    <tr>        <td align="center" width=100px>v:shell_error</td>        <td align="left">上一次 Shell 命令的结果，如果为0，则命令正常执行，如果非0，则命令执行失败</td>    </tr></table>
              </div>
            </details>

<h2 id="3-列出变量"><a href="#3-列出变量" class="headerlink" title="3. 列出变量"></a><font size=3>3. 列出变量</font></h2><p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:let</span><br></pre></td></tr></table></figure>

<p>这将会列出所有变量的值，变量的类型在值之前给出。例如，</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-04-%E5%8F%98%E9%87%8F%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F/img/image-20220306100412807.png" alt="image-20220306100412807" style="zoom:50%;" />

<p>其中，<code>&lt;空&gt;</code>代表字符串，<code>#</code>代表数值，<code>*</code>代表函数引用。</p>
<h2 id="4-删除变量"><a href="#4-删除变量" class="headerlink" title="4. 删除变量"></a><font size=3>4. 删除变量</font></h2><p>定义了一个变量，用完想要销毁的话，该怎么操作呢？在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:unl[et][!] &#123;name&#125;   &quot; 删除内部变量 &#123;name&#125;</span><br><span class="line">:unl[et] $&#123;env-name&#125; &quot; 删除环境变量 &#123;env-name&#125;</span><br></pre></td></tr></table></figure>

<p>这个命令用于删除内部变量 <code>&#123;var-name&#125;</code>。可以给出多个变量的名字，它们都会被删除。该名字也可以是<code>List</code>或 <code>Dictionary</code>项目。如果使用<code> [!]</code>，即使变量不存在也不会给出错误。</p>
<p>在一个 <code>:unlet</code> 命令里可以混合 <code>&#123;name&#125;</code> 和 <code>$&#123;env-name&#125;</code>。不对不存在的变量报错，即使没有 <code>!</code> 也是如此。如果系统不支持删除环境变量，则将它清空。</p>
<h2 id="5-变量详解"><a href="#5-变量详解" class="headerlink" title="5. 变量详解"></a><font size=3>5. 变量详解</font></h2><h3 id="5-1-数值Number"><a href="#5-1-数值Number" class="headerlink" title="5.1 数值Number"></a><font size=3>5.1 数值<code>Number</code></font></h3><ul>
<li>十进制<code>Decimalism</code></li>
</ul>
<p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo 105</span><br></pre></td></tr></table></figure>

<p>这个时候，<code>vim</code>底部会显示数字<code>105</code>，这就是一个十进制的整数。</p>
<ul>
<li>十六进制<code>Hexadecimal</code></li>
</ul>
<p>我们可以加<code>0x</code>前缀来使用十六进制数，在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo 0x7a</span><br></pre></td></tr></table></figure>

<p>这个时候，<code>vim</code>底部会显示数字<code>122</code>，这是一个十进制的整数，而我们打印的数是一个十六进制数，换算过来，<code>0x7a = 122</code>。</p>
<ul>
<li>八进制<code>Octal</code></li>
</ul>
<p>我们可以加<code>0</code>前缀来使用八进制数，在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:echo 035</span><br><span class="line">:echo 039</span><br></pre></td></tr></table></figure>

<p>这个时候会发现，第一条命令显示<code>29</code>，这是一个标准的八进制数转换成的十进制，第三条命令显示<code>39</code>，因为八进制数只有<code>0-7</code>，<code>vim</code>并不认它是一个八进制数。</p>
<h3 id="5-2-浮点数Float"><a href="#5-2-浮点数Float" class="headerlink" title="5.2 浮点数Float"></a><font size=3>5.2 浮点数<code>Float</code></font></h3><ul>
<li>直接写小数</li>
</ul>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo 100.123</span><br></pre></td></tr></table></figure>

<p>会发现，在<code>vim</code>底部输出<code>100.123</code>。</p>
<ul>
<li>指数表示</li>
</ul>
<p>当数量级很大或者很小的时候，<code>vim</code>提供指数的表示形式，用<code>e</code>表示<code>10</code>的多少次幂，<code>+</code>和<code>-</code>表示是正指数还是负指数。</p>
<p>【注意】<code>+</code>和<code>-</code>是可选的，不写的时候默认是正指数；小数点前后一定要有数字，且小数点前的数字最好不要超过<code>10</code>。</p>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:echo 5.25e+3  &quot; 将输出 5250.0</span><br><span class="line">:echo 5.25e-3  &quot; 将输出 0.00525</span><br><span class="line">:echo 3.25e3   &quot; 将输出 3250.0</span><br><span class="line">:echo 13.25e3  &quot; 将输出 13250.0</span><br></pre></td></tr></table></figure>

<p>依次执行，第一条命令显示<code>5250.0</code>，第二条命令显示<code>0.00525</code>，第三条命令显示<code>3250.0</code>，第四条命令等价于<code> 1.325e4</code>，显示<code>13250.0</code>。</p>
<h3 id="5-3-强制转换"><a href="#5-3-强制转换" class="headerlink" title="5.3 强制转换"></a><font size=3>5.3 强制转换</font></h3><ul>
<li>乘法</li>
</ul>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:echo 2 * 2      &quot; 将输出 4</span><br><span class="line"></span><br><span class="line">:echo 2 * 2.0    &quot; 将输出 4.0</span><br><span class="line">:echo 2.0 * 2.0  &quot; 将输出 4.0</span><br></pre></td></tr></table></figure>

<ul>
<li>除法</li>
</ul>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">:echo 3 / 2      &quot; 将输出 1</span><br><span class="line"></span><br><span class="line">:echo 3 / 2.0    &quot; 将输出 1.5</span><br><span class="line">:echo 3.0 / 2    &quot; 将输出 1.5</span><br><span class="line">:echo 3.0 / 2.0  &quot; 将输出 1.5</span><br></pre></td></tr></table></figure>

<p>加减法也是如此，当有浮点数参与运算的时候，参与运算的数都被强制转化为浮点数，最后的结果也会是浮点数。没有浮点数参与的时候，也不会凭空产生小数，就像<code>3/2</code>结果是<code>1.5</code>但是，<code>vim</code>会自动去掉余数，只显示整数。</p>
<h3 id="5-4-字符串String"><a href="#5-4-字符串String" class="headerlink" title="5.4 字符串String"></a><font size=3>5.4 字符串<code>String</code></font></h3><p>在<code>vim</code>中字符串有两种，一种使用<code>&quot; &quot;</code>包裹起来，这种字符串会对其中的转义字符(<code>\</code>作为标志)进行转义；另一种使用<code>&#39; &#39;</code>包裹起来，这种字符串就适合于想原样打印字符串，但内部却又偏偏包含了转义字符。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&quot;string&quot;    &quot; 会对转义字符进行转义</span><br><span class="line">&#x27;string&#x27;    &quot; 所有字符原样输出</span><br></pre></td></tr></table></figure>

<h4 id="5-4-1-转义字符打印"><a href="#5-4-1-转义字符打印" class="headerlink" title="5.4.1 转义字符打印"></a><font size=3>5.4.1 转义字符打印</font></h4><p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo &quot;Hello\nWorld\n!&quot;</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Hello </span><br><span class="line">World</span><br><span class="line">!</span><br></pre></td></tr></table></figure>

<p>可以发现，打印结果分了三行，这是因为中间有两个换行符<code>\n</code>。</p>
<h4 id="5-4-2-原样打印字符串"><a href="#5-4-2-原样打印字符串" class="headerlink" title="5.4.2 原样打印字符串"></a><font size=3>5.4.2 原样打印字符串</font></h4><p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo &#x27;Hello\nWorld\n!&#x27;</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Hello\nWorld\n!</span><br></pre></td></tr></table></figure>

<h4 id="5-4-3-连接字符串"><a href="#5-4-3-连接字符串" class="headerlink" title="5.4.3 连接字符串"></a><font size=3>5.4.3 连接字符串</font></h4><p>&emsp;在<code>vim</code>中用于连接两个字符串的符号是<code>.</code>。在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo &quot;Hello &quot; . &quot;World&quot; . &quot;!&quot;</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Hello World!</span><br></pre></td></tr></table></figure>

<h4 id="5-4-4-截取字符串"><a href="#5-4-4-截取字符串" class="headerlink" title="5.4.4 截取字符串"></a><font size=3>5.4.4 截取字符串</font></h4><p><code>vimscript</code>中支持通过索引来截取字符串，格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;12345678&quot;[n1:n2] &quot; n1 n2 表示首尾两个索引</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）<code>n1</code>省略表示首字符索引，<code>n2</code>省略时默认为结尾字符索引。</p>
<p>（2）当<code>[]</code>内只有一个数字时，可以访问字符串下某一个位置的字符，即通过<code>&quot;string&quot;[n]</code>可以访问第<code>n</code>个字符，<code>n</code>从<code>0</code>开始，是一个正值，<strong>若为负值，则会没有报错地得到一个空字符</strong>。</p>
<p>（3）<code>n1</code>和<code>n2</code>可以为负值，但是，为负值的时候，<code>-1</code>代表最后一个字符，<code>-2</code>代表倒数第二个字符，以此类推。</p>
<ul>
<li>访问某个字符</li>
</ul>
<p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:let str = &quot;12345&quot;</span><br><span class="line">:echo str[0] str[-1] str[4]</span><br></pre></td></tr></table></figure>

<p>运行结果为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1  5</span><br></pre></td></tr></table></figure>

<ul>
<li>截取字符串</li>
</ul>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:let str = &quot;12345&quot;</span><br><span class="line">:echo str[:2] str[0:2] str[3:] str[3:4]</span><br></pre></td></tr></table></figure>

<p>运行结果为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">123 123 45 45 </span><br></pre></td></tr></table></figure>

<p>接下来尝试一下负索引，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:let str = &quot;12345&quot;</span><br><span class="line">:echo str[-3:-2] str[-3:] str[0:-3]</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">34 345 123</span><br></pre></td></tr></table></figure>

<h4 id="5-4-5-字符串函数"><a href="#5-4-5-字符串函数" class="headerlink" title="5.4.5 字符串函数"></a><font size=3>5.4.5 字符串函数</font></h4><p>&emsp;（1）求字符串长度函数<code>strlen</code>，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:echo strlen(&quot;fanhua\n&quot;) &quot; 将输出 7</span><br><span class="line">:echo strlen(&#x27;fanhua\n&#x27;) &quot; 将输出 8</span><br></pre></td></tr></table></figure>

<p>然后我们会在<code>vim</code>底部看到，第一条命令会输出一个<code>7</code>，第二条命令会输出一个<code>8</code>。</p>
<p>（2）切割字符串函数<code>split</code>，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo split(&quot;one two three&quot;)</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[&#x27;one&#x27;, &#x27;two&#x27;, &#x27;three&#x27;]</span><br></pre></td></tr></table></figure>

<p>可以看到，函数将字符串按空格切割成了一个列表。那想要按其他方式切割呢？在命令模式中执行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo split(&quot;one two three&quot;,&quot;t&quot;)</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[&#x27;one &#x27;, &#x27;wo &#x27;, &#x27;hree&#x27;]</span><br></pre></td></tr></table></figure>

<p>我们发现，按<code>t</code>将字符串再次分割成了一个列表，但是<code>t</code>字符会被去掉。</p>
<p>字符串切割函数<code>split</code>可以在第二个参数中指定以什么标志进行切割，不写的时候默认是按空格进行切割，切割后，该函数会将各部分放入一个列表进行返回。</p>
<p>（3）连接字符串函数<code>join</code>，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:echo join(split(&quot;123456 qq.com&quot;), &quot;@&quot;)</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">123456@qq.com</span><br></pre></td></tr></table></figure>

<p>这个命令的含义就是，先将里边的字符串按空格切开，然后再将各部分之间加上<code>@</code>符号，再将其拼接成一个字符串。</p>
<p>还有很多其他的函数，有需要的时候可以来这里搜一搜：<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#functions">VIM: eval - functions <i class="fa fa-external-link-alt"></i></a></p>
<h3 id="5-5-列表List"><a href="#5-5-列表List" class="headerlink" title="5.5 列表List"></a><font size=3>5.5 列表<code>List</code></font></h3><p>列表是有序的，异质的元素集合。官方文档中关于列表的介绍在这里：<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#Lists">VIM: eval - Lists<i class="fa fa-external-link-alt"></i></a></p>
<h4 id="5-5-1-创建列表"><a href="#5-5-1-创建列表" class="headerlink" title="5.5.1 创建列表"></a><font size=3>5.5.1 创建列表</font></h4><p>在命令模式执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:let emptylist = []</span><br><span class="line">:let mylist = [1, two, 3, &quot;four&quot;]</span><br><span class="line">:let nestlist = [[11, 12], [21, 22], [31, 32]]</span><br></pre></td></tr></table></figure>

<p>即可创建一个列表，列表可以为空，也可以有值，列表的项目可以是任何表达式，甚至是列表，就如第三条命令，就创建了一个列表的列表，产生了列表的嵌套。</p>
<h4 id="5-5-2-列表索引"><a href="#5-5-2-列表索引" class="headerlink" title="5.5.2 列表索引"></a><font size=3>5.5.2 列表索引</font></h4><p>上边已经知道了如何创建一个列表，那么，想要引用列表中的值，该怎么引用呢？在列表之后的<strong>方括号中放上索引号</strong>可以访问列表项目。<strong>索引从零开始</strong>，也就是说，第一个 项目的索引值为零。 </p>
<ul>
<li>正值索引</li>
</ul>
<p>接下来我们来尝试正常访问列表，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;, [3, &#x27;a&#x27;]]</span><br><span class="line">&quot; 访问列表值</span><br><span class="line">:echo mylist[0] mylist[1] mylist[2] mylist[2][0] mylist[2][1]</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 two [3, &#x27;a&#x27;] 3 a</span><br></pre></td></tr></table></figure>

<p>当访问列表时可按照下边格式访问，当列表嵌套列表的时候，依次加索引就可以进行访问。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list_name[index]              &quot; 无嵌套列表</span><br><span class="line">list_name[index1][index2]...  &quot; 有嵌套列表</span><br></pre></td></tr></table></figure>

<ul>
<li>负值索引</li>
</ul>
<p>接下来，我们尝试一下，索引为负值会是什么情况呢？在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;, [3, &#x27;a&#x27;]]</span><br><span class="line">&quot; 访问列表值</span><br><span class="line">:echo mylist[0] mylist[-1] mylist[-2] mylist[-1][0] mylist[-1][-1]</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 [3, &#x27;a&#x27;] two 3 a</span><br></pre></td></tr></table></figure>

<p>可以发现，索引为<code>-1</code>时，访问到的时最后一个元素，<code>-2</code>是倒数第二个元素。</p>
<ul>
<li>打印整个列表</li>
</ul>
<p>有的时候，我们会需要打印出整个列表来查看列表中的数据情况，这个时候，我们只要输入列表名称即可打印出整个列表，在命令模式执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;, [3, &#x27;a&#x27;]]</span><br><span class="line">&quot; 访问列表值</span><br><span class="line">:echo mylist</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1, &#x27;two&#x27;, [3, &#x27;a&#x27;]]</span><br></pre></td></tr></table></figure>

<h4 id="5-5-3-修改元素"><a href="#5-5-3-修改元素" class="headerlink" title="5.5.3 修改元素"></a><font size=3>5.5.3 修改元素</font></h4><p>通过以下格式进行修改或者赋值：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:let List_name[index] = value</span><br></pre></td></tr></table></figure>

<p>【注意】元素修改时，索引不可超限。</p>
<p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:let mylist = [1, 2]</span><br><span class="line">:let mylist[1] = &quot;two&quot;</span><br><span class="line">:echo mylist</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1, &#x27;two&#x27;]</span><br></pre></td></tr></table></figure>

<h4 id="5-5-4-列表连接"><a href="#5-5-4-列表连接" class="headerlink" title="5.5.4 列表连接"></a><font size=3>5.5.4 列表连接</font></h4><p>在<code>vim</code>中支持将两个列表连接到一起，连接两个列表的符号为<code>+</code>。在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;]</span><br><span class="line">:let longlist = mylist + [3, &#x27;a&#x27;]</span><br><span class="line">:echo longlist mylist</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1, &#x27;two&#x27;, 3, &#x27;a&#x27;] [1, &#x27;two&#x27;]</span><br></pre></td></tr></table></figure>

<p>可以发现，两个列表连接在了一起，新的列表包含了原来两个列表中的元素。</p>
<h4 id="5-5-5-子列表"><a href="#5-5-5-子列表" class="headerlink" title="5.5.5 子列表"></a><font size=3>5.5.5 子列表</font></h4><p><code>vim</code>还支持截取子列表，就是列表的一部分。子列表可以通过指定首末两个索引获得，方括号内以冒号分隔两者。格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List_name[n1:n2]   &quot; n1 n2 表示首尾两个索引</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）首索引<code>n1</code>的省略类似于用 0，末索引<code>n2</code>的省略类似于用 <code>-1</code>。 </p>
<p>（2）列表首末两个索引<code>n1</code>和<code>n2</code>可以为负值。</p>
<p>（3）<code>n1</code>和<code>n2</code>越过列表索引上界也是安全的。</p>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;, [3, &#x27;a&#x27;]]</span><br><span class="line">&quot; 访问列表值</span><br><span class="line">:echo mylist[:1] mylist[1:] mylist[-2:-1]</span><br></pre></td></tr></table></figure>

<p>运行结果如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1, &#x27;two&#x27;] [&#x27;two&#x27;, [3, &#x27;a&#x27;]] [&#x27;two&#x27;, [3, &#x27;a&#x27;]]</span><br></pre></td></tr></table></figure>

<h4 id="5-5-6-列表同一"><a href="#5-5-6-列表同一" class="headerlink" title="5.5.6 列表同一"></a><font size=3>5.5.6 列表同一</font></h4><p>如果变量 <code>mylist1</code> 是列表，把它赋给另一个变量 <code>mylist2</code> 后，两个变量将指向同一列表。因此， 对列表<code>mylist1</code> 的修改也同时修改了<code>mylist2</code>。</p>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:let mylist1 = [1, 2, 3]</span><br><span class="line">:let mylist2 = mylist1</span><br><span class="line">:let mylist2[1] = 9</span><br><span class="line">:echo &quot;mylist1=&quot;mylist1&quot;\nmylist2=&quot;mylist2</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mylist1= [1, 9, 3] </span><br><span class="line">mylist2= [1, 9, 3]</span><br></pre></td></tr></table></figure>

<p>单纯的想要备份列表怎么办呢？我们可以使用<code>deepcopy()</code>函数来创建一个列表的备份，它们将完全独立。</p>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:let mylist1 = [1, 2, 3]</span><br><span class="line">:let mylist2 = deepcopy(mylist1)</span><br><span class="line">:let mylist2[1] = 9</span><br><span class="line">:echo &quot;mylist1=&quot;mylist1&quot;\nmylist2=&quot;mylist2</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mylist1= [1, 2, 3] </span><br><span class="line">mylist2= [1, 9, 3]</span><br></pre></td></tr></table></figure>

<h4 id="5-5-7-列表解包"><a href="#5-5-7-列表解包" class="headerlink" title="5.5.7 列表解包"></a><font size=3>5.5.7 列表解包</font></h4><p>什么是列表解包？就是把列表中的每个元素拿一个单独的变量给存起来。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">:let [var1, var2; rest] = list</span><br><span class="line">&quot; 等价写法</span><br><span class="line">:let var1 = list[0]</span><br><span class="line">:let var2 = list[1]</span><br><span class="line">:let rest = list[2:]</span><br></pre></td></tr></table></figure>

<p>【说明】当前边变量数等于后边列表中元素数量的时候，不会报错，<code>rest</code>将成为空表，这个时候<code>;rest</code>也可以省略掉。</p>
<h4 id="5-5-8-列表函数"><a href="#5-5-8-列表函数" class="headerlink" title="5.5.8 列表函数"></a><font size=3>5.5.8 列表函数</font></h4><ul>
<li>求列表长度函数<code>len</code></li>
</ul>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;, [3, &#x27;a&#x27;]]</span><br><span class="line">:echo len(mylist) len(mylist[-1])</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">3 2</span><br></pre></td></tr></table></figure>

<p>整个列表有三个元素，所以打印数字<code>3</code>，第最后一个元素为列表，列表内有两个元素，所以打印数字<code>2</code>。</p>
<ul>
<li>列表增加元素函数<code>add</code></li>
</ul>
<p>在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;]</span><br><span class="line">:let newlist = add(mylist, &#x27;b&#x27;)</span><br><span class="line">:echo newlist</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1, &#x27;two&#x27;, &#x27;b&#x27;]</span><br></pre></td></tr></table></figure>

<ul>
<li>返回特定元素索引<code>index</code></li>
</ul>
<p><code>index</code>函数返回给定项在给定列表的第一个索引，如果不在列表中则返回<code>-1</code>。在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个列表</span><br><span class="line">:let mylist = [1, &quot;two&quot;, &#x27;b&#x27;]</span><br><span class="line">:echo index(mylist, &#x27;b&#x27;) index(mylist, &#x27;a&#x27;)</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2 -1</span><br></pre></td></tr></table></figure>

<h3 id="5-6-字典Dictionary"><a href="#5-6-字典Dictionary" class="headerlink" title="5.6 字典Dictionary"></a><font size=3>5.6 字典<code>Dictionary</code></font></h3><p>字典是关联数组，每个项目有一个键和一个值。官方文档中关于列表的介绍在这里：<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#Dictionaries">VIM: eval - Dictionaries <i class="fa fa-external-link-alt"></i> </a></p>
<h4 id="5-6-1-创建字典"><a href="#5-6-1-创建字典" class="headerlink" title="5.6.1 创建字典"></a><font size=3>5.6.1 创建字典</font></h4><p>字典通过花括号里逗号分隔的项目列表建立，每个项目包含以冒号分隔的键和值，一个键 只能出现一次。在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:let emptydict = &#123;&#125;</span><br><span class="line">:let mydict = &#123;1: &#x27;one&#x27;, 2: &#x27;two&#x27;, 3: &#x27;three&#x27;&#125;</span><br><span class="line">:let nestdict = &#123;1: &#123;11: &#x27;a&#x27;, 12: &#x27;b&#x27;&#125;, 2: &#123;21: &#x27;c&#x27;&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>【说明】</p>
<p>（1）键必须是字符串，用数值也可以，但它总被自动转换为字符串。所以字符串<code>&#39;4&#39;</code>和数值 <code>4 </code>总会找到相同的项目。</p>
<p>（2）值可以是任何表达式，如果值本身是字典，就可以建立嵌套的字典</p>
<p>（3）如果不想给每个键都加上引号，可以用 <code>#&#123;&#125;</code> 形式。这要求键只包含<code>ASCII</code>字母、数位、<code>-</code>和<code> _</code>，并且这种格式下不允许存在空键。例如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:let mydict = #&#123;zero: 0, one_key: 1, 333: 3&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-6-2-字典索引"><a href="#5-6-2-字典索引" class="headerlink" title="5.6.2 字典索引"></a><font size=3>5.6.2 字典索引</font></h4><ul>
<li>使用<code>[]</code>来访问</li>
</ul>
<p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个字典</span><br><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:echo mydict[&#x27;a&#x27;] mydict[&#x27;b&#x27;]</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">one 2</span><br></pre></td></tr></table></figure>

<ul>
<li>使用<code>.</code>来访问</li>
</ul>
<p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个字典</span><br><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:echo mydict.a mydict.b</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">one 2</span><br></pre></td></tr></table></figure>

<ul>
<li>打印整个字典</li>
</ul>
<p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个字典</span><br><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:echo mydict</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-6-3-字典修改"><a href="#5-6-3-字典修改" class="headerlink" title="5.6.3 字典修改"></a><font size=3>5.6.3 字典修改</font></h4><p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个字典</span><br><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:let mydict.a = 1</span><br><span class="line">:let mydict[&#x27;b&#x27;] = &quot;two&quot;</span><br><span class="line">:echo &quot;mydict=&quot;mydict</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mydict= &#123;&#x27;a&#x27;: 1, &#x27;b&#x27;: &#x27;two&#x27;&#125;</span><br></pre></td></tr></table></figure>

<p>在字典中，找到相应的键后直接赋值，就可以达到修改键值的目的。</p>
<h4 id="5-6-4-字典添加"><a href="#5-6-4-字典添加" class="headerlink" title="5.6.4 字典添加"></a><font size=3>5.6.4 字典添加</font></h4><p>在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个字典</span><br><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:let mydict.c = &quot;three&quot;</span><br><span class="line">:echo &quot;mydict=&quot;mydict</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mydict= &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2, &#x27;c&#x27;: &#x27;three&#x27;&#125;</span><br></pre></td></tr></table></figure>

<p>在字典中，可以直接添加元素，直接写入键和值即可。</p>
<h4 id="5-6-5-字典移除"><a href="#5-6-5-字典移除" class="headerlink" title="5.6.5 字典移除"></a><font size=3>5.6.5 字典移除</font></h4><p>有的时候需要删除某个字典元素，该怎么做呢？</p>
<ul>
<li><code>remove</code></li>
</ul>
<p><code>remove</code>函数将移除给定字典的给定键对应的项，并<strong>返回被移除的值</strong>，不能移除不在字典中的项。在命令模式执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot; 创建一个字典</span><br><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:let test = remove(mydict, &#x27;a&#x27;)</span><br><span class="line">:echo &quot;mydict=&quot;mydict&quot;\ntest=&quot;test</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mydict= &#123;&#x27;b&#x27;: 2&#125; </span><br><span class="line">test= one</span><br></pre></td></tr></table></figure>

<ul>
<li><code>unlet</code></li>
</ul>
<p><code>unlet</code>命令也能移除字典中的项，只是不返回值，并且不能移除字典中不存在的项。在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:let mydict = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:unlet mydict[&#x27;a&#x27;]</span><br><span class="line">:echo &quot;mydict=&quot;mydict</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mydict= &#123;&#x27;b&#x27;: 2&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-6-6-字典同一"><a href="#5-6-6-字典同一" class="headerlink" title="5.6.6 字典同一"></a><font size=3>5.6.6 字典同一</font></h4><p>与列表同一类似。在命令模式下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:let mydict1 = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:let mydict2 = mydict1</span><br><span class="line">:let mydict2.a = 1</span><br><span class="line">:echo &quot;mydict1=&quot;mydict1&quot;\nmydict2=&quot;mydict2</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mydict1= &#123;&#x27;a&#x27;: 1, &#x27;b&#x27;: 2&#125; </span><br><span class="line">mydict2= &#123;&#x27;a&#x27;: 1, &#x27;b&#x27;: 2&#125;</span><br></pre></td></tr></table></figure>

<p>当我们使用<code>deepcopy()</code>函数进行字典复制时，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:let mydict1 = &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125;</span><br><span class="line">:let mydict2 = deepcopy(mydict1)</span><br><span class="line">:let mydict2.a = 1</span><br><span class="line">:echo &quot;mydict1=&quot;mydict1&quot;\nmydict2=&quot;mydict2</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mydict1= &#123;&#x27;a&#x27;: &#x27;one&#x27;, &#x27;b&#x27;: 2&#125; </span><br><span class="line">mydict2= &#123;&#x27;a&#x27;: 1, &#x27;b&#x27;: 2&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-6-7-字典函数"><a href="#5-6-7-字典函数" class="headerlink" title="5.6.7 字典函数"></a><font size=3>5.6.7 字典函数</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">:if has_key(dict, &#x27;foo&#x27;)        &quot; 如果 dict 有带 &quot;foo&quot; 键的项目则为真</span><br><span class="line">:if empty(dict)                 &quot; 如果 dict 为空则为真</span><br><span class="line">:let l = len(dict)              &quot; dict 项目的数量</span><br><span class="line">:let big = max(dict)            &quot; dict 项目的最大值</span><br><span class="line">:let small = min(dict)          &quot; dict 项目的最小值</span><br><span class="line">:let xs = count(dict, &#x27;x&#x27;)      &quot; 统计 dict 里 &#x27;x&#x27; 出现的数目</span><br></pre></td></tr></table></figure>

<h2 id="6-查看变量类型"><a href="#6-查看变量类型" class="headerlink" title="6. 查看变量类型"></a><font size=3>6. 查看变量类型</font></h2><p>有的时候，我们需要查看变量的类型怎么办呢？<code>vim</code>为我们提供了一个函数<code>type()</code>来查看变量类型。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">type(&#123;expr&#125;) &quot; 返回数值，代表 &#123;expr&#125; 的类型</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看返回值含义 </summary>
              <div class='content'>
              <table>    <tr><td align="center" width=90px>返回值</td><td align="center">说明</td><td align="center">含有此值的 v:t_ 变量</td></tr>    <tr><td align="center" width=90px>0 </td><td align="left">数值</td><td align="left">v:t_number </td></tr>    <tr><td align="center" width=90px>1 </td><td align="left">字符串</td><td align="left">v:t_string </td></tr>    <tr><td align="center" width=90px>2 </td><td align="left">函数引用</td><td align="left">v:t_func </td></tr>    <tr><td align="center" width=90px>3 </td><td align="left">列表</td><td align="left">v:t_list </td></tr>    <tr><td align="center" width=90px>4 </td><td align="left">字典</td><td align="left">v:t_dict </td></tr>    <tr><td align="center" width=90px>5 </td><td align="left">浮点数</td><td align="left">v:t_float </td></tr>    <tr><td align="center" width=90px>6 </td><td align="left">布尔值</td><td align="left">v:t_bool  (v:false 和 v:true)</td></tr>    <tr><td align="center" width=90px>7 </td><td align="left">None</td><td align="left">v:t_none  (v:null 和 v:none)</td></tr>    <tr><td align="center" width=90px>8 </td><td align="left">作业</td><td align="left">v:t_job </td></tr>    <tr><td align="center" width=90px>9 </td><td align="left">通道</td><td align="left">v:t_channel </td></tr>    <tr><td align="center" width=90px>10</td><td align="left">blob</td><td align="left">v:t_blob </td></tr></table><p>【说明】最好不要直接使用返回值，最好用含有此返回值的 <code>v:t_ </code>变量。</p>
              </div>
            </details>

<h1 id="二、vim表达式"><a href="#二、vim表达式" class="headerlink" title="二、vim表达式"></a><font size=3>二、<code>vim</code>表达式</font></h1><p>学习完变量，接下来就该表达式了，这在<code>vimscript</code>中也是极为重要的。</p>
<p>另外在此说明一下，之前运行命令的时候都是在命令模式中运行，对于多行的命令运行起来就显得很繁琐，所以后边涉及到的测试脚本都将使用脚本文件<code>test.vim</code>来进行。</p>
<details class="folding-tag" blue><summary> 点击查看表达式优先级 </summary>
              <div class='content'>
              <p>【说明】表格来自于<code>vim</code>文档《<a target="_blank" rel="noopener" href="http://vimcdoc.sourceforge.net/doc/eval.html#expression-syntax">VIM: eval - expression-syntax <i class="fa fa-external-link-alt"></i> </a>》一节。</p><table>    <tr><td align="center" width=90px>优先级</td><td align="center">表达式</td><td align="center">说明</td></tr>    <tr><td align="center" width=90px rowspan="15">1(expr9)</td><td align="left" width=200px>number</td><td align="left">数值常数</td></tr>    <tr><td align="left" width=200px>"string"</td><td align="left">字符串常数，反斜杠有特殊含义</td></tr>    <tr><td align="left" width=200px>'string'</td><td align="left">字符串常数，' 加倍</td></tr>    <tr><td align="left" width=200px>[expr1, ...]</td><td align="left"> List </td></tr>    <tr><td align="left" width=200px>{expr1: expr1, ...}</td><td align="left"> Dictionary </td></tr>    <tr><td align="left" width=200px>#{key: expr1, ...}</td><td align="left"> Dictionary </td></tr>    <tr><td align="left" width=200px>&option</td><td align="left">选项值</td></tr>    <tr><td align="left" width=200px>(expr1)</td><td align="left">嵌套表达式</td></tr>    <tr><td align="left" width=200px>variable</td><td align="left">内部变量</td></tr>    <tr><td align="left" width=200px>va{ria}ble</td><td align="left">带花括号的内部变量</td></tr>    <tr><td align="left" width=200px>$VAR</td><td align="left">环境变量</td></tr>    <tr><td align="left" width=200px>@r</td><td align="left">寄存器 'r' 的值</td></tr>    <tr><td align="left" width=200px>function(expr1, ...)</td><td align="left">函数调用</td></tr>    <tr><td align="left" width=200px>func{ti}on(expr1, ...)</td><td align="left">带花括号的函数调用</td></tr>    <tr><td align="left" width=200px>{args -> expr1}</td><td align="left">匿名函数表达式</td></tr>    <tr><td align="center" width=90px rowspan="5">2(expr8)</td><td align="left" width=200px>expr8[expr1]</td><td align="left">字符串里的字节或者  List  的项目</td></tr>    <tr><td align="left" width=200px>expr8[expr1 : expr1]</td><td align="left">字符串子串或  List  的子列表</td></tr>    <tr><td align="left" width=200px>expr8.name</td><td align="left"> Dictionary  的项目</td></tr>    <tr><td align="left" width=200px>expr8(expr1, ...)</td><td align="left">使用  Funcref  变量的函数调用</td></tr>    <tr><td align="left" width=200px>expr8->name(expr1, ...)</td><td align="left"> method  调用</td></tr>    <tr><td align="center" width=90px rowspan="3">3(expr7)</td><td align="left" width=200px>! expr7</td><td align="left">逻辑非，把  TRUE  变为  FALSE </td></tr>    <tr><td align="left" width=200px>- expr7</td><td align="left">一元减法: 取反，改变数值的符号。</td></tr>    <tr><td align="left" width=200px>+ expr7</td><td align="left">一元加法: 原值</td></tr>    <tr><td align="center" width=90px rowspan="3">4(expr6)</td><td align="left" width=200px>expr7 *  expr7 ...</td><td align="left">数值乘法</td></tr>    <tr><td align="left" width=200px>expr7 /  expr7 ...</td><td align="left">数值除法</td></tr>    <tr><td align="left" width=200px>expr7 %  expr7 ...</td><td align="left">数值求余</td></tr>    <tr><td align="center" width=90px rowspan="4">5(expr5)</td><td align="left" width=200px>expr6 +  expr6 ...</td><td align="left">数值加法、列表或 blob 连接</td></tr>    <tr><td align="left" width=200px>expr6 -  expr6 ...</td><td align="left">数值减法</td></tr>    <tr><td align="left" width=200px>expr6 .  expr6 ...</td><td align="left">字符串连接</td></tr>    <tr><td align="left" width=200px>expr6 .. expr6 ...</td><td align="left">字符串连接</td></tr>    <tr><td align="center" width=90px rowspan="12">6(expr4)</td><td align="left" width=200px>expr5 == expr5</td><td align="left">等于</td></tr>    <tr><td align="left" width=200px>expr5 != expr5</td><td align="left">不等于</td></tr>    <tr><td align="left" width=200px>expr5 >  expr5</td><td align="left">大于</td></tr>    <tr><td align="left" width=200px>expr5 >= expr5</td><td align="left">大于等于</td></tr>    <tr><td align="left" width=200px>expr5 <  expr5</td><td align="left">小于</td></tr>    <tr><td align="left" width=200px>expr5 <= expr5</td><td align="left">小于等于</td></tr>    <tr><td align="left" width=200px>expr5 =~ expr5</td><td align="left">匹配正则表达式</td></tr>    <tr><td align="left" width=200px>expr5 !~ expr5</td><td align="left">不匹配正则表达式</td></tr>    <tr><td align="left" width=200px>expr5 ==? expr5</td><td align="left">等于，忽略大小写</td></tr>    <tr><td align="left" width=200px>expr5 ==# expr5</td><td align="left">等于，匹配大小写</td></tr>    <tr><td align="left" width=200px>expr5 is expr5</td><td align="left">相同的  List 、 Dictionary  或  Blob  实例</td></tr>    <tr><td align="left" width=200px>expr5 isnot expr5</td><td align="left">不同的  List 、 Dictionary  或  Blob  实例</td></tr>    <tr><td align="center" width=90px>7(expr3)</td><td align="left" width=200px>expr4 && expr4 ...</td><td align="left">逻辑与</td></tr>    <tr><td align="center" width=90px>8(expr2)</td><td align="left" width=200px>expr3 || expr3 ...</td><td align="left">逻辑或</td></tr>    <tr><td align="center" width=90px>9(expr1)</td><td align="left" width=200px>expr2 ? expr1 : expr1</td><td align="left">if-then-else</td></tr></table><p>【注意】</p><p>（1）同一层的表达式从左到右进行分析。</p><p>（2）<code>...</code> 标明这一层上的操作可以连接，如<code>&amp;nu || &amp;list &amp;&amp; &amp;shell == &quot;csh&quot;</code>。</p>
              </div>
            </details>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/31c064e2.html">LV02-vimscript-04-变量与表达式.md</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 15:05</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/31c064e2.html" title="LV02-vimscript-04-变量与表达式.md">https://sumumm.github.io/post/31c064e2.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/VIM/" rel="tag"><i class="fa fa-tag"></i> VIM</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/bd34b04e.html" rel="prev" title="LV02-vimscript-05-语句.md">
                  <i class="fa fa-angle-left"></i> LV02-vimscript-05-语句.md
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/65fb9432.html" rel="next" title="LV02-vimscript-03-状态条.md">
                  LV02-vimscript-03-状态条.md <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
